// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "DBManager/Repository/HDF5/Input/Material/Orthotropic2DMaterialRepository.h"

#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"

HDF5_REG_REPOSITORY (Orthotropic2DMaterialRepository)

using namespace SG::DBManager::HDF5;
using namespace SG::DataStructure;

Orthotropic2DMaterialRepository::Orthotropic2DMaterialRepository (HDF5Database& database)
    : SingleRepositoryBase<FEM::Orthotropic2DMaterial> (database)
{}

void Orthotropic2DMaterialRepository::InitialDatasetInfo ()
{
    RegisterIndex ("/OWN/INDEX/INPUT/MATERIAL/MAT8", IndexDataset, DataType::GetType ());
    RegisterTableInfo<FEM::Orthotropic2DMaterial> ("/SG/INPUT/MATERIAL/MAT8", MainDataset);
    // RegisterTableInfo<MAT8::MAT8HSN> ("/SG/INPUT/MATERIAL/MAT8HSN", SubDataset);

    // 连接信号槽 当调用汇总的时候，将已有的缓存信息全部都持久化到HDF5文件中。
    GetDatabase ().GetHDF5Hub()->m_materialSummarySignal.connect ([this] { Sort (); });
}

// void Orthotropic2DMaterialRepository::DumpWriteCahce ()
// {
//     auto& writeStorage = this->GetMainCache ().m_writeStorage;
//     GetMainDatasetInfo ().SetIsSequential (false);

//     std::vector<MAT8::IDENTITY> IDENTITYData;
//     std::vector<MAT8::MAT8HSN>  subDataset;

//     OwnToNastran (writeStorage, IDENTITYData, subDataset);

//     WriteData (MainDataset, IDENTITYData);
//     WriteData (SubDataset, subDataset);

//     // 更新Index表
//     WriteIndexData (this->GetIndexDatasetInfo (), writeStorage);

//     writeStorage.clear ();
// }
// void Orthotropic2DMaterialRepository::LoadReadCache (std::size_t loadIndex)
// {
//     std::vector<MAT8::IDENTITY> IDENTITYData;
//     LoadMainMetadata (loadIndex, IDENTITYData);

//     std::vector<MAT8::MAT8HSN> subDataset;
//     GetDataset (SubDataset).select ({ GetMainCache ().dataMinIndex }, { IDENTITYData.size () }).read (subDataset);

//     auto* cacheVector = m_cacheHelper.LookupOrCreateVector<DataType> (GetMainCache ().m_readCacheKey, IDENTITYData.size ());
    // auto* cacheVector = m_cacheHelper.LookupOrCreateVector<DataType> (GetMainCache ().m_readCacheKey, IDENTITYData.size ());
    // NastranToOwn (IDENTITYData, subDataset, *cacheVector);
    // return cacheVector;
// }
// void Orthotropic2DMaterialRepository::SortData ()
// {
//     std::vector<MAT8::IDENTITY> IDENTITYData;
//     GetDataset (MainDataset).read (IDENTITYData);

//     std::vector<MAT8::MAT8HSN> subDataset;
//     GetDataset (SubDataset).read (subDataset);

//     // 将所有数据转换
//     std::vector<FEM::Orthotropic2DMaterial> AllData{};
//     NastranToOwn (IDENTITYData, subDataset, AllData);

//     std::sort (AllData.begin (), AllData.end ());

//     OwnToNastran (AllData, IDENTITYData, subDataset);

//     // 写出数据至文件
//     GetDataset (MainDataset).write (IDENTITYData);
//     GetDataset (SubDataset).write (subDataset);

//     // 重写索引
//     RewriteIndexData (AllData);

//     // 设置为已顺序
//     GetMainDatasetInfo ().SetIsSequential (true);
// };

// void Orthotropic2DMaterialRepository::OwnToNastran (std::vector<SG::DataStructure::FEM::Orthotropic2DMaterial>& own,
//                                                     std::vector<SG::DBManager::HDF5::MAT8::IDENTITY>& _OUT      IDENTITYData,
//                                                     std::vector<SG::DBManager::HDF5::MAT8::MAT8HSN>& _OUT       subData)
// {
//     IDENTITYData.resize (own.size ());

//     for (size_t i = 0; i < own.size (); i++)
//     {
//         // 添置数据至主表
//         auto&& OwnIdent = own[i];
//         auto&& NasIdent = IDENTITYData[i];
//         // 因为字段一致，直接拷贝数据过来
//         std::memcpy (&NasIdent, &OwnIdent, sizeof (MAT8::IDENTITY));

//         // 添加数据至附表
//         SG::DBManager::HDF5::MAT8::MAT8HSN hsn;
//         hsn.MID        = OwnIdent.m_id;
//         hsn.m_domainId = OwnIdent.m_domainId;
//         hsn.HF1        = OwnIdent.m_HFAILFactor[0];
//         hsn.HF2        = OwnIdent.m_HFAILFactor[1];
//         hsn.HF3        = OwnIdent.m_HFAILFactor[2];
//         hsn.HF4        = OwnIdent.m_HFAILFactor[3];
//         hsn.HF10       = OwnIdent.m_HFAILFactor[4];
//         hsn.HF11       = OwnIdent.m_HFAILFactor[5];

//         subData.emplace_back (hsn);
//     }
// }

// void Orthotropic2DMaterialRepository::NastranToOwn (std::vector<SG::DBManager::HDF5::MAT8::IDENTITY>&                IDENTITYData,
//                                                     std::vector<SG::DBManager::HDF5::MAT8::MAT8HSN>&                 subData,
//                                                     std::vector<SG::DataStructure::FEM::Orthotropic2DMaterial>& _OUT own)
// {}
